
if (SERVER) then

	AddCSLuaFile( "shared.lua" )
	SWEP.Weight				= 5
	SWEP.AutoSwitchTo		= false
	SWEP.AutoSwitchFrom		= false

end

if ( CLIENT ) then

	SWEP.DrawAmmo			= true
	SWEP.DrawCrosshair		= false
	SWEP.ViewModelFOV		= 82
	SWEP.ViewModelFlip		= true
	SWEP.CSMuzzleFlashes	= true

end

SWEP.Author			= "Azaz3l"
SWEP.Contact		= ""
SWEP.Purpose		= ""
SWEP.Instructions	= ""

/*--------------------------
Primary Functions Init
---------------------------*/
SWEP.Primary.Sound = Sound("weapons/smg1/smg1-1.wav")
SWEP.Primary.Recoil = 0.15
SWEP.Primary.Tracer = true
SWEP.Primary.Spread = 0.2
SWEP.Primary.Damage = 40
SWEP.Primary.CanFireUnderWater = true
SWEP.Primary.Automatic = true
SWEP.Hold =  "ar2"
SWEP.BulSlowDistance = 120
SWEP.MinSpread = 0.000
SWEP.MaxSpread = 0.10
SWEP.AddSpread = 0.1
SWEP.CoolDownTime = 0.5
SWEP.Primary.ClipSize		= 30
SWEP.Primary.DefaultClip	= 60
SWEP.Primary.Ammo			= "ar2"
SWEP.IronSightModifier  = 0.7
SWEP.JumpModifier = 1.6
SWEP.WalkModifier = 1.3
SWEP.CrouchModifier = 0.6
SWEP.MuzzleEffect			= "muzzle_rifle"
SWEP.ViewPunch     = 1
SWEP.ViewKick = 0.5
SWEP.MuzzleAttachment		= "1"
SWEP.Caliber                = "7.62x39mm"
SWEP.RPM = 800
SWEP.ScopeTex = "sprites/scope_detailed"
SWEP.FASReload = true
SWEP.UseScope = false
SWEP.Zoom = 2.1
SWEP.BullNum = 1

SWEP.SprintPos = Vector (7.4355, -18.3718, -1.1893)
SWEP.SprintAng = Vector (-7.172, 60.486, 1.7106)

/*---------------------------------------------------------
---------------------------------------------------------*/
function SWEP:Initialize()
	self:SetWeaponHoldType( self.Hold )
	if ( SERVER ) then
	self:SetNPCMinBurst( 30 )
	self:SetNPCMaxBurst( 30 )
	self:SetNPCFireRate( 0.01 )
	end
	
	self.Weapon:SetNetworkedBool( "Ironsights", false )
	self.CurSpread = self.MinSpread
	local SpreadMod = 1
	self.SprintWeaponDisable = false --Disable Weapons When Sprinting
	gap = 50
	mm = 20
	self.Primary.Delay = 60/self.RPM
end


/*---------------------------------------------------------
	Reload does nothing
---------------------------------------------------------*/
function SWEP:Reload()
local clip = self.Weapon:Clip1()
self:ResetIS()
if clip == self.Primary.ClipSize then return end
if self.FASReload == true then
if clip > 0 then
self.Weapon:DefaultReload( ACT_VM_RELOAD )
elseif clip == 0 or clip < 0 then 
self.Weapon:DefaultReload( ACT_VM_RELOAD_EMPTY )
self:ResetIS()
end
else
self.Weapon:DefaultReload( ACT_VM_RELOAD )
end
end

function SWEP:Think()	
if self.UseScope == false then
self:IronSight()
else
self:Scope()
end
end

function SWEP:IronSight()


	if self.Owner:KeyPressed(IN_ATTACK2) and self.SprintWeaponDisable == false then
	-- When the right click is pressed, then

self.Owner:SetFOV( 55, 0.15 )
self:SetIronsights(true, self.Owner)
	-- Set the ironsight true
self.Owner:SetWalkSpeed(150)
self.Owner:SetRunSpeed(150)
if CLIENT then return end
end

if self.Owner:KeyReleased(IN_ATTACK2) and self.SprintWeaponDisable == false then
	-- If the right click is released, then

self.Owner:SetFOV( 0, 0.15 )
self:SetIronsights(false, self.Owner) -- Set the ironsight false
self.Owner:SetWalkSpeed(250)
self.Owner:SetRunSpeed(500)
if CLIENT then return end
end
end
/*---------------------------------------------------------
	PrimaryAttack
---------------------------------------------------------*/
SWEP.LastAtt = CurTime()
function SWEP:PrimaryAttack()
self.Weapon:SetNextPrimaryFire(CurTime()+self.Primary.Delay)
if !self:CanPrimaryAttack() then return end
self:BaseAttack()
end
function SWEP:ShootWeaponEffects()
local PlayerPos = self.Owner:GetShootPos()
local PlayerAim = self.Owner:GetAimVector()

self.Weapon:EmitSound(self.Primary.Sound)
self.Weapon:SendWeaponAnim(ACT_VM_PRIMARYATTACK) 		-- View model animation
self.Owner:SetAnimation(PLAYER_ATTACK1)	-- 3rd Person Animation
self.Owner:MuzzleFlash()
local fx = EffectData()
fx:SetEntity(self.Weapon)
fx:SetOrigin(PlayerPos)
fx:SetNormal(PlayerAim)
fx:SetAttachment(self.MuzzleAttachment)
util.Effect(self.MuzzleEffect,fx)						-- Additional muzzle effects
end

function SWEP:ResetIS()

self.Owner:SetFOV( 0, 0.15 )
self:SetIronsights(false, self.Owner) -- Set the ironsight false
self.Owner:SetWalkSpeed(250)
self.Owner:SetRunSpeed(500)
self.Weapon:SetNetworkedBool("Scoped",false)
if SERVER then
self.Owner:DrawViewModel(true)
end
end

function SWEP:ApplySpread()

self.Weapon:SetNetworkedFloat( "LastShootTime", CurTime() )
local CT = CurTime()
local RecTime = CT - self.LastAtt
local RecoverScale = (1 - RecTime/self.CoolDownTime)
self.LastAtt = CT

--Adding Spread
self.CurSpread = math.Clamp(self.CurSpread + self.AddSpread, self.MinSpread , self.MaxSpread)
	-- Apply cool-down
self.CurSpread = math.Clamp(self.CurSpread*RecoverScale, self.MinSpread, self.MaxSpread)

end

local IRONSIGHT_TIME = 0.25

/*---------------------------------------------------------
   Name: GetViewModelPosition
   Desc: Allows you to re-position the view model
---------------------------------------------------------*/
function SWEP:GetViewModelPosition( pos, ang )

	if ( !self.IronSightsPos ) then return pos, ang end

	local bIron = self.Weapon:GetNetworkedBool( "Ironsights" )
	
	if ( bIron != self.bLastIron ) then
	
	self.bLastIron = bIron 
	self.fIronTime = CurTime()
	if ( bIron ) then 
	self.SwayScale 	= 0.3
	self.BobScale 	= 0.1
	else 
	self.SwayScale 	= 1.0
	self.BobScale 	= 1.0
end
	
	end
	
	local fIronTime = self.fIronTime or 0

	if ( !bIron && fIronTime < CurTime() - IRONSIGHT_TIME ) then 
		return pos, ang 
	end
	
	local Mul = 1.0
	
	if ( fIronTime > CurTime() - IRONSIGHT_TIME ) then
	
	Mul = math.Clamp( (CurTime() - fIronTime) / IRONSIGHT_TIME, 0, 1 )
	if (!bIron) then Mul = 1 - Mul end
	
	end

	local Offset	= self.IronSightsPos
	
	if ( self.IronSightsAng ) then
	
	ang = ang * 1
	ang:RotateAroundAxis( ang:Right(), 		self.IronSightsAng.x * Mul )
	ang:RotateAroundAxis( ang:Up(), 		self.IronSightsAng.y * Mul )
	ang:RotateAroundAxis( ang:Forward(), 	self.IronSightsAng.z * Mul )
	
	
	end
	
	local Right 	= ang:Right()
	local Up 		= ang:Up()
	local Forward 	= ang:Forward()
	
	

	pos = pos + Offset.x * Right * Mul
	pos = pos + Offset.y * Forward * Mul
	pos = pos + Offset.z * Up * Mul

	return pos, ang
	
end


/*---------------------------------------------------------
	SetIronsights
---------------------------------------------------------*/
function SWEP:SetIronsights( b )

	self.Weapon:SetNetworkedBool( "Ironsights", b )

end

local IRONSIGHT_TIME = 0.25

SWEP.NextSecondaryAttack = 0
/*---------------------------------------------------------
	SecondaryAttack
---------------------------------------------------------*/
function SWEP:SecondaryAttack()
end


/*---------------------------------------------------------
	onRestore
	Loaded a saved game (or changelevel)
---------------------------------------------------------*/
function SWEP:OnRestore()

	self.NextSecondaryAttack = 0
	self:SetIronsights( false )
	
end

function SWEP:CheckClip()
local clip = self.Weapon:Clip1()
if clip == 0 then return false
end
end

function SWEP:CheckSpreadModifiers(walk,iron,jump,crouch)
local SpreadMod = 1

	if self.Owner:KeyDown(IN_FORWARD | IN_BACK | IN_MOVELEFT | IN_MOVERIGHT) then
	SpreadMod = SpreadMod*walk
	end
	
	if !self.Owner:IsOnGround() then
	SpreadMod = SpreadMod*jump 
	end
	
	if self.Weapon:GetNetworkedBool("Ironsights", false) then 
	SpreadMod = SpreadMod*iron
	end
	
	if self.Owner:Crouching() and self.Owner:IsOnGround() then 
	SpreadMod = SpreadMod*crouch
	end
	
	return SpreadMod

end


function SWEP:DrawHUD()
if self.Weapon:GetNetworkedBool( "Scoped" ) == true then
	surface.SetTexture(surface.GetTextureID(self.ScopeTex))
	surface.SetDrawColor(0,0,0,255)
	surface.DrawTexturedRect(0,0,ScrW(),ScrH())
	surface.SetDrawColor( 0, 0, 0, 255 )
	surface.DrawLine( 0, ScrH() / 2, ScrW(), ScrH() / 2)
	surface.DrawLine( ScrW() / 2, 0, ScrW() / 2, ScrH())
end
	
if self.Weapon:GetNetworkedBool( "Ironsights" ) or self.Weapon:GetNWBool("Scoped") then return end
if self.Owner:KeyPressed(IN_ATTACK) then
mm = 15
end
	local x = ScrW()/2
	local y = ScrH()/2
	local scale = 10 * self.CrossHairScale
	// Scale the size of the crosshair according to how long ago we fired our weapon
	local LastShootTime = self.Weapon:GetNetworkedFloat( "LastShootTime", 0 )
	if self.Owner:KeyDown(IN_FORWARD) or self.Owner:KeyDown(IN_BACK) or self.Owner:KeyDown(IN_MOVELEFT) or self.Owner:KeyDown(IN_MOVERIGHT) then
	gap = math.Clamp(gap+0.4,15,27.5)
	mm = 27.5
	elseif self.Owner:KeyDown(IN_DUCK) then
	gap = math.Clamp(gap-0.4,10,mm)
	else
	gap = math.Clamp(gap-0.4,15,mm)
	end
	gap = gap * (2 - math.Clamp( (CurTime() - LastShootTime)*5, 0, 1 ))
	local length = gap + 50 * scale

	surface.SetDrawColor( 225,225,225, 250)
	surface.DrawRect( x+length+gap/length-13.5, y, 15, 2 )
	surface.DrawRect( x-length, y, 15, 2 )
	surface.DrawRect( x, y-length, 2, 15 )
	surface.DrawRect( x, y+length+gap/length-13.5,2, 15 )
	
	end
	
	function SWEP:ShootBulletSlow()
local PlayerAim = self.Owner:GetAimVector()
local PlayerPos = self.Owner:GetShootPos()

	numbul = self.BullNum or 1
	mask = mask or MASK_SHOT -- Tracemask
	
if SERVER then
	for i=1,numbul do
	
	local eBullet = ents.Create(self.Caliber)

	local tBullet = {} -- This is the bullet our bullet SENT will be firing when it hits something.  Everything except force and damage is determined by the bullet SENT
	tBullet.Force	= 0.15*self.Primary.Damage
	tBullet.Damage	= self.Primary.Damage

	local tTrace = {} --This is the trace the bullet SENT uses to see if it has hit something
	tTrace.filter = filter or {self.Owner,eBullet}
	tTrace.mask = mask

	eBullet:SetVar("Trace",tTrace)
	eBullet:SetVar("Owner",self.Owner)
	eBullet:Spawn()

	if (SERVER) then
	
	eBullet:SetPos( PlayerPos )

	eBullet:SetAngles(PlayerAim:Angle()+(Angle(math.Rand(-self.CurSpread*100,self.CurSpread*100),math.Rand(-self.CurSpread*75,self.CurSpread*75),math.Rand(-self.CurSpread*100,self.CurSpread*100))*SpreadMod))
	eBullet:Spawn()
	end
	eBullet:Spawn()
	end
	end

	end
	

function SWEP:BaseAttack()

local tr = self.Owner:GetEyeTrace()
SpreadMod = self:CheckSpreadModifiers(self.WalkModifier, self.IronSightModifier, self.JumpModifier, self.CrouchModifier)
self:TakePrimaryAmmo(1)
self.Weapon:EmitSound(self.Primary.Sound)
self:ApplySpread()
self:ShootBulletSlow()
self.Owner:ViewPunchReset()
self.Owner:ViewPunch(Angle(math.Rand(0,-2.5) * (self.ViewPunch), math.Rand(-1,1) * (self.ViewPunch), 0))
self:ShootWeaponEffects()
local eyeang = self.Owner:EyeAngles()
eyeang.pitch = eyeang.pitch - math.Rand(self.ViewKick-0.2,self.ViewKick)
eyeang.yaw = eyeang.yaw - math.Rand((self.ViewKick-0.2)/3,self.ViewKick/3)
self.Owner:SetEyeAngles( eyeang )

end

function SWEP:CanSecondaryAttack()
 
	if ( self:Clip2() <= 0 ) then
 
	self:EmitSound( "Weapon_Pistol.Empty" )
	self:SetNextSecondaryFire( CurTime() + 0.2 )
	return false
 
	end
 
	return true
 
end


function SWEP:CanSecondaryAttack()
 
	if ( self:Clip1() <= 0 ) then
 
	self:EmitSound( "Weapon_Pistol.Empty" )
	return false
 
	end
 
	return true
 
end

function SWEP:Deploy()

self.Weapon:SendWeaponAnim(ACT_VM_DEPLOY)
gap = 50

end

function SWEP:Holster(wep)
self:ResetIS()
return true
end


function SWEP:Scope() 
if self.Owner:KeyPressed(IN_ATTACK2) then
self:SetIronsights(true, self.Owner)
self.Owner:SetFOV( self.Owner:GetFOV()/self.Zoom, 0.15 )
self.Owner:SetWalkSpeed(100)
self.Owner:SetRunSpeed(100)
self.Weapon:SetNetworkedBool("Scoped",true)
if SERVER then 
self.Owner:DrawViewModel(false)
end
end
if self.Owner:KeyReleased(IN_ATTACK2) then

self.Owner:SetFOV( 0, 0.15 )
self.Weapon:SetNetworkedBool("Scoped",false)
self:SetIronsights(false, self.Owner)
self.Owner:SetWalkSpeed(250)
self.Owner:SetRunSpeed(500)
if SERVER then 
self.Owner:DrawViewModel(true)
end
end
end